package com.gzw.scm.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import com.gzw.scm.R;
import com.gzw.scm.bean.BWInfo;

import java.util.HashMap;
import java.util.List;


/**
 * Created by gaozhiwei on 2015/10/10.
 */
public class BWAdapter extends BaseAdapter {
    private Context mContext;
    private List<BWInfo> list;
    private static HashMap<Integer, Boolean> isSelected;

    public BWAdapter(Context _mContext, List<BWInfo> list) {
        mContext = _mContext;
        this.list = list;
        isSelected = new HashMap<Integer, Boolean>();
        initDate();

    }

    // 初始化isSelected的数据
    private void initDate() {
        for (int i = 0; i < list.size(); i++) {
            getIsSelected().put(i, false);
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder mHolder = null;
        if (convertView == null) {
            convertView = View
                    .inflate(mContext, R.layout.item_gv_bw, null);
            mHolder = new Holder(convertView);
            convertView.setTag(mHolder);
        } else {
            mHolder = (Holder) convertView.getTag();
        }
        BWInfo bwInfo = list.get(position);
        mHolder.tvName.setText(bwInfo.getName());
        final Holder finalMHolder = mHolder;
        mHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                finalMHolder.cb.setSelected(isChecked);
                isSelected.put(position, isChecked);
            }
        });        
        return convertView;
    }
    static class Holder {
        private TextView tvName;
        private CheckBox cb;

        public Holder(View view) {
            tvName = (TextView) view.findViewById(R.id.tv_name);
            cb = (CheckBox) view.findViewById(R.id.cb);
        }
    }

    public static HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        BWAdapter.isSelected = isSelected;
    }

}